package com.ea.eadp.nexus.proxy.tests.cart;

import com.ea.eadp.test.shared.workflow.identity.PostPidWorkflowElement;
import com.ea.eadp.test.shared.workflow.nexus.PostAnonymousAccessTokenWorkflowElement;
import com.ea.eadp.test.shared.workflow.nexus.PostNewAccessTokenWorkflowElement;
import com.ea.eadp.test.shared.workflow.nexus.proxy.cart.PostAddAnonymousOffersWorkflowElement;
import com.ea.eadp.test.shared.workflow.nexus.proxy.cart.PostAddOffersWorkflowElement;

/**
 * Created by YimingCao on 9/1/2015.
 */
public abstract class BaseClass {
    public BaseClass()
    {
    }

    protected String defaultOfferId = "CART-OFR-002-01";
    protected String defaultPromoRuleId = "CART-PROMOTION-100-PERCENT-OFF";

    public NamedCart getANamedCart()
    {
        PostPidWorkflowElement postPidWorkflowElement = new PostPidWorkflowElement();
        postPidWorkflowElement.execute();

        PostNewAccessTokenWorkflowElement postNewAccessTokenWorkflowElement = new PostNewAccessTokenWorkflowElement();
        postNewAccessTokenWorkflowElement.input.client_id = "cart_service_for_originx_test";
        postNewAccessTokenWorkflowElement.input.client_secret = "secret1";
        postNewAccessTokenWorkflowElement.input.email = postPidWorkflowElement.input.email;
        postNewAccessTokenWorkflowElement.input.password = postPidWorkflowElement.input.password;
        postNewAccessTokenWorkflowElement.input.grant_type = "password";
        postNewAccessTokenWorkflowElement.execute();

        PostAddOffersWorkflowElement postAddOffersWorkflowElement = new PostAddOffersWorkflowElement();
        postAddOffersWorkflowElement.input.pidId = String.valueOf(postPidWorkflowElement.output.pidId);
        postAddOffersWorkflowElement.input.isAnonymous = Boolean.FALSE;
        postAddOffersWorkflowElement.input.accessToken = postNewAccessTokenWorkflowElement.output.response.getBody();
        postAddOffersWorkflowElement.addOffer(defaultOfferId, 2);
        postAddOffersWorkflowElement.execute();

        NamedCart namedCart = new NamedCart();
        namedCart.setCartName(postAddOffersWorkflowElement.input._cartName);
        namedCart.setAccessToken(postAddOffersWorkflowElement.input.accessToken);
        namedCart.setPid(String.valueOf(postPidWorkflowElement.output.pidId));
        namedCart.setEntityInCartContainer(postAddOffersWorkflowElement.output.entityInCartContainer);

        return namedCart;
    }

    public AnonymousCart getAnAnonymousCart()
    {
        PostAnonymousAccessTokenWorkflowElement postAnonymousAccessTokenWorkflowElement = new PostAnonymousAccessTokenWorkflowElement();
        postAnonymousAccessTokenWorkflowElement.input.client_id = "cart_service_for_originx_test";
        postAnonymousAccessTokenWorkflowElement.execute();

        PostAddAnonymousOffersWorkflowElement postAddAnonymousOffersWorkflowElement = new PostAddAnonymousOffersWorkflowElement();
        postAddAnonymousOffersWorkflowElement.input.accessToken = postAnonymousAccessTokenWorkflowElement.output.response.getBody();
        postAddAnonymousOffersWorkflowElement.addOffer(defaultOfferId, 2);
        postAddAnonymousOffersWorkflowElement.execute();

        AnonymousCart anonymousCart = new AnonymousCart();
        anonymousCart.setAnonymousAccessToken(postAnonymousAccessTokenWorkflowElement.output.response.getBody());
        anonymousCart.setCartName(postAddAnonymousOffersWorkflowElement.input._cartName);
        anonymousCart.setFakePid(postAddAnonymousOffersWorkflowElement.output.response.getBody().getCartInfo().getPidId().toString());
        anonymousCart.setEntityInCartContainer(postAddAnonymousOffersWorkflowElement.output.entityInCartContainer);

        return anonymousCart;
    }
}
